package main;

import sdf.df.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Main extends Activity {
	WebView webview;
	EditText addressbar;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// initializing world class, see W.init() for details
		W.init(getApplicationContext());

		// setting homepageview
		setContentView(R.layout.main);

		// addressbar
		addressbar = (EditText) findViewById(R.id.addressbar_edittext);
		addressbar.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				loadAddress();
				return false;
			}
		});

		// Go button
		Button goButton = (Button) findViewById(R.id.b_go);
		goButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				loadAddress();
			}
		});

		// Web view
		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {

				// String js = W.getRawResourceLink(getApplicationContext(),
				// "script.js");
				// String css = W.getRawResourceLink(getApplicationContext(),
				// "style.css");
				//
				// Log.e("", js);
				// Log.e("", css);
				// webview.loadUrl(css);
				// webview.loadUrl(js);
				super.onPageFinished(view, url);

			}
		});
		// webview.loadData(html, "text/html", "utf-8");
		Log.e("", W.getFileAddress("index.html"));
		// webview.loadUrl(W.getFileAddress("index.html"));
		webview.loadData(W.readTextFile(W.getFileAddress("index.php")),
				"text/html", "utf-8");

	}

	void loadAddress() {
		String address = addressbar.getText().toString();
		if (!address.startsWith("http://")) {
			address = new String("http://").concat(address);
		}
		// if (!address.endsWith(".com")) {
		// address = address.concat(".com");
		// }
		webview.loadUrl(address);
	}
}